1 module hip.loaders.texture_atlas;
2 import hip.filesystem.hipfs;
3 import hip.asset_manager.load_task;
4 import hip.assetmanager;
5 import hip.api.data.commons;
6 import hip.console.log;
7 
8 /**
9 *   Returns a load task for a texture atlas
10 *   If ":IGNORE" is provided for texturePath, the following behavior will occur:
11 *   - .json: Will try to load a file with same name but with extension .png
12 *   - .atlas: texturePath is always ignored
13 *   - .txt(or any): Load a file with same name but extension .png
14 *   - .xml: Ignore internal texture path to try file with same name but .png extension
15 */
16 final class HipTextureAtlasLoadTask : HipAssetLoadTask
17 {
18     private HipFSPromise fs;
19     protected Image img;
20     string texturePath = ":IGNORE";
21 
22     this(string path, string name, HipAsset asset, string texturePath, const(ubyte)[] extraData, string fileRequesting, size_t lineRequesting)
23     {
24         super(path, name, asset, extraData, fileRequesting, lineRequesting);
25         this.texturePath = texturePath;
26     }
27 
28     override void update()
29     {
30         final switch(result) with (HipAssetResult)
31         {
32             case waiting:
33                 result = loading;
34                 worker = HipAssetManager.loadWorker("Load and Decode TextureAtlas", ()
35                 {
36                     HipFS.read(path)
37                     .addOnError((string err){error = err; result = cantLoad;})
38                     .addOnSuccess((in ubyte[] data)
39                     {
40                         HipTextureAtlas tAtlas = HipTextureAtlas.readFromMemory(data, path, texturePath);
41                         asset = tAtlas;
42 
43                         HipFS.read(tAtlas.getTexturePath())
44                         .addOnError((string err){error = err; result = cantLoad;})
45                         .addOnSuccess((in ubyte[] imgData)
46                         {
47                             new Image(tAtlas.getTexturePath(), imgData, (IImage self)
48                             {
49                                 img = cast(Image)self;
50                                 result = mainThreadLoading;
51                             }, (){result = cantLoad; error = "Could not load image for TextureAtlas";});
52                             return FileReadResult.free;
53                         });
54                         return FileReadResult.free;
55                     });
56                 });
57                 break;
58             case loading:
59                 break;
60             case mainThreadLoading:
61                 HipTextureAtlas atlas = cast(HipTextureAtlas)asset;
62                 if(!atlas.loadTexture(img))
63                 {
64                     error = "Could not load Texture from TextureAtlas at ";
65                     result = cantLoad;
66                     return;
67                 }
68                 result = loaded;
69                 break;
70             case cantLoad: goto case loaded;
71             case loaded:
72                 break;
73         }
74     }
75 
76 }